package admin

import (
	"backend_api/internal/model/request"
	"backend_api/internal/service"
	"fmt"
	"github.com/gin-gonic/gin"
)

type AdminTools struct{}

// GetToolList 获取工具列表
func (h *AdminTools) GetToolList(c *gin.Context) {
	var req request.ToolListReq
	if err := c.ShouldBindQuery(&req); err != nil {
		ResponseError(c, err)
		return
	}
	srv := service.ToolService{}
	list, total, err := srv.GetToolList(&req)
	if err != nil {
		ResponseError(c, err)
		return
	}

	ResponseSuccess(c, gin.H{
		"list":  list,
		"total": total,
	})
}

// AddTool 添加工具
func (h *AdminTools) AddTool(c *gin.Context) {
	var req request.ToolAddReq
	if err := c.ShouldBindJSON(&req); err != nil {
		ResponseError(c, err)
		return
	}

	srv := service.ToolService{}
	if err := srv.AddTool(&req); err != nil {
		ResponseError(c, err)
		return
	}

	ResponseSuccess(c, nil)
}

// UpdateTool 更新工具
func (h *AdminTools) UpdateTool(c *gin.Context) {
	var req request.ToolUpdateReq
	if err := c.ShouldBindJSON(&req); err != nil {
		ResponseError(c, err)
		return
	}

	srv := service.ToolService{}
	if err := srv.UpdateTool(&req); err != nil {
		ResponseError(c, err)
		return
	}

	ResponseSuccess(c, nil)
}

// DeleteTool 删除工具
func (h *AdminTools) DeleteTool(c *gin.Context) {
	var req request.ToolDeleteReq
	if err := c.ShouldBindJSON(&req); err != nil {
		ResponseError(c, err)
		return
	}

	srv := service.ToolService{}
	if err := srv.DeleteTool(&req); err != nil {
		ResponseError(c, err)
		return
	}

	ResponseSuccess(c, nil)
}

func (h *AdminTools) UpdateStatus(c *gin.Context) {
	var req request.ToolUpdateStatusReq
	fmt.Println(req)
	if err := c.ShouldBindJSON(&req); err != nil {
		ResponseError(c, err)
		return
	}

	srv := service.ToolService{}
	if err := srv.UpdateStatus(&req); err != nil {
		ResponseError(c, err)
		return
	}

	ResponseSuccess(c, nil)
}

func (h *AdminTools) UpdateSort(c *gin.Context) {
	var req request.ToolUpdateSortReq
	fmt.Println(req)
	if err := c.ShouldBindJSON(&req); err != nil {
		ResponseError(c, err)
		return
	}

	srv := service.ToolService{}
	if err := srv.UpdateSort(&req); err != nil {
		ResponseError(c, err)
		return
	}

	ResponseSuccess(c, nil)
}
